import React, { Component } from 'react';
import { connect } from 'dva';
import { Row, Col, Card, Form, Input, Divider,
  Button, Table, Modal, Popconfirm } from 'antd';
import styles from './List.less';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import AddDistrict from './AddDistrict';
import ShowCommunity from './ShowCommunityInfo';
import globalConstant from '../../utils/globalConstant';

const FormItem = Form.Item;

@connect(({ district, loading }) => ({
  district,
  loading: loading.models.district,
}))
@Form.create()
export default class DistrictManagement extends Component {
  state = {
    showDistrictAddVisible: false,
    showDistrictVisible: false,
    districtObj: {},
    pageNum: globalConstant.paging.defaultPageNum,
    pageSize: globalConstant.paging.defaultPageSize,
    form: {},
  }
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'district/getDistrictList',
      payload: {
        pageNum: globalConstant.paging.defaultPageNum,
        pageSize: globalConstant.paging.defaultPageSize,
      },
    });
  }
  handleStandardTableChange = (pagination) => {
    const { dispatch } = this.props;
    const params = {
      pageNum: pagination.current,
      pageSize: pagination.pageSize,
      ...this.state.form,
    };
    this.setState({
      pageNum: params.pageNum,
      pageSize: params.pageSize,
    });
    dispatch({
      type: 'district/getDistrictList',
      payload: params,
    });
  }
  handleSearch = (e) => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      dispatch({
        type: 'district/getDistrictList',
        payload: {
          keywordsValue: fieldsValue.keywordsValue,
        },
      });
      this.setState({
        form: {
          keywordsValue: fieldsValue.keywordsValue,
        },
      });
    });
  }
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      form:{},
    });
    dispatch({
      type: 'district/getDistrictList',
      payload: {
        pageNum: globalConstant.paging.defaultPageNum,
        pageSize: globalConstant.paging.defaultPageSize,
      },
    });
  }
  handleAddMasterModal= () => {
    this.setState({
      showDistrictAddVisible: true,
    });
  }
  handleDistrictAddModalCancel =(refreshFlag) => {
    if (refreshFlag) {
      this.props.dispatch({
        type: 'district/getDistrictList',
        payload: {
          pageNum: this.state.pageNum,
          pageSize: this.state.pageSize,
          ...this.state.form,
        },
      });
    }
    this.setState({
      showDistrictAddVisible: false,
    });
  }
  handleShowDistrictModal= (value) => {
    this.setState({
      showDistrictVisible: true,
      districtObj: value,
    });
  }
  handleShowDistrictModalCancel =() => {
    this.setState({
      showDistrictVisible: false,
    });
  }
  handleChildModalClose = () => {
    this.setState({
      showDistrictVisible: false,
    });
  }
  handleDeleteDistrict =(value) => {
    this.props.dispatch({
      type: 'district/delDistrict',
      payload: {
        id: value.id,
      },
      callback: () => {
        this.props.dispatch({
          type: 'district/getDistrictList',
          payload: {
            pageNum: this.state.pageNum,
            pageSize: this.state.pageSize,
            ...this.state.form,
          },
        });
      },
    });
  }
  renderSimpleForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ xs: 8, sm: 16, md: 24 }}>
          <Col md={8} sm={8}>
            <FormItem label="关键字">
              {getFieldDecorator('keywordsValue')(
                <Input placeholder="请输入搜索关键字" />
              )}
            </FormItem>
          </Col>
          <Col span={8}>
            <span className={styles.submitButtons}>
              <Button icon="search" type="primary" htmlType="submit" >查询</Button>
              <Button icon="reload" style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }
  render() {
    const { district: { districtList }, loading } = this.props;
    const { showDistrictAddVisible, showDistrictVisible, districtObj } = this.state;
    const columns = [
      {
        title: '编号',
        dataIndex: 'id',
        width: '33%',
      },
      {
        title: '区域名称',
        dataIndex: 'districtName',
        width: '33%',
      },
      {
        title: '操作',
        key: 'key',
        render: (text, record, index) => {
          return (
            <div key={index.toString()}>
              <Button type="primary" onClick={() => { this.handleShowDistrictModal(text); }} >
                管理小区
              </Button>
              <Divider type="vertical" />
              <Popconfirm title="是否删除？" onConfirm={() => { this.handleDeleteDistrict(text); }}>
                <Button type="danger" >
                  删除纪录
                </Button>
              </Popconfirm>
            </div>
          );
        },
      },
    ];
    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      total: districtList.totals,
      showTotal: (total, range) => `${range[0]}-${range[1]} of ${total} items`,
      ...globalConstant.pagingFormat,
    };
    return (
      <PageHeaderLayout title="区域管理">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>
              {this.renderSimpleForm()}
            </div>
            <Card bordered>
              <div className={styles.tableListOperator}>
                <Button icon="plus" type="primary" onClick={() => this.handleAddMasterModal()}>
                  新建
                </Button>
              </div>
              <Table
                bordered
                loading={loading}
                dataSource={districtList.items}
                rowKey={record => record.id}
                columns={columns}
                pagination={paginationProps}
                onChange={this.handleStandardTableChange}
              />
            </Card>
          </div>
        </Card>
        <Modal
          visible={showDistrictAddVisible}
          title="添加区域"
          footer={null}
          width="60%"
          onCancel={() => this.handleDistrictAddModalCancel()}
          destroyOnClose
        >
          <AddDistrict handleAddModal={this.handleDistrictAddModalCancel.bind(this)} />
        </Modal>
        <Modal
          visible={showDistrictVisible}
          title="管理小区"
          footer={null}
          width="60%"
          onCancel={() => this.handleShowDistrictModalCancel()}
          destroyOnClose
        >
          <ShowCommunity
            childObject={this.handleChildModalClose.bind(this)}
            districtObj={districtObj}
          />
        </Modal>
      </PageHeaderLayout>
    );
  }
}
